package com.linchuansi;

import com.linchuansi.ioc.BeanDefinition;
import com.linchuansi.ioc.DefaultBeanFactory;
import com.linchuansi.ioc.GeneralBeanDefinition;
import com.linchuansi.project.Factory;
import com.linchuansi.project.UserService;
import org.junit.jupiter.api.Test;

/**
 * *
 *
 * @author wb
 * @date 2025-11-24 20:13*@since 1.0.0
 */
public class DemoSpring {
    @Test
    public void test() throws Exception {
        createInstanceByConstructor();
    }
    @Test
    public void aliasTest() throws Exception {
        DefaultBeanFactory beanFactory = new DefaultBeanFactory();
        GeneralBeanDefinition beanDefinition = new GeneralBeanDefinition();
        beanDefinition.setBeanClass(UserService.class);
        beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETION);
        beanDefinition.setInitMethodName("init");
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        Object userService1 = beanFactory.getBean("userService");
        System.out.println(userService1);
        Object userService2= beanFactory.getBean("userService");
        System.out.println(userService2);
        System.out.println(userService1==userService2);
        beanFactory.registerAlias("userService","userService1");
        beanFactory.registerAlias("userService","userService2");
        beanFactory.registerAlias("userService","userService3");
        beanFactory.registerAlias("userService","userService4");
        String userService11 = beanFactory.getOriginalBeanName("userService1");
        System.out.println(beanFactory.getAliases("userService"));
        beanFactory.removeAlias("userService1");
        beanFactory.removeAlias("userService2");
        System.out.println(beanFactory.getAliases("userService"));
        System.out.println(userService11);

    }
    private void createInstanceByConstructor() throws Exception {
        DefaultBeanFactory beanFactory = new DefaultBeanFactory();
        GeneralBeanDefinition beanDefinition = new GeneralBeanDefinition();
        beanDefinition.setBeanClass(UserService.class);
        beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETION);
        beanDefinition.setInitMethodName("init");
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        Object userService1 = beanFactory.getBean("userService");
        System.out.println(userService1);
        Object userService2= beanFactory.getBean("userService");
        System.out.println(userService2);
        System.out.println(userService1==userService2);
    }
    private void createInstanceByStaticFactoryMethod() throws Exception {
        DefaultBeanFactory beanFactory = new DefaultBeanFactory();
        GeneralBeanDefinition beanDefinition = new GeneralBeanDefinition();
        beanDefinition.setBeanClass(Factory.class);
        beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETION);
        beanDefinition.setFactoryMethodName("getUserService");
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        Object userService1 = beanFactory.getBean("userService");
        System.out.println(userService1);
        Object userService2= beanFactory.getBean("userService");
        System.out.println(userService2);
        System.out.println(userService1==userService2);
    }
    private void createInstanceByInstanceFactoryMethod() throws Exception {
        DefaultBeanFactory beanFactory = new DefaultBeanFactory();
        GeneralBeanDefinition factoryBeanDefinition = new GeneralBeanDefinition();
        factoryBeanDefinition.setBeanClass(Factory.class);
        factoryBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETION);
        beanFactory.registerBeanDefinition("factory", factoryBeanDefinition);
        GeneralBeanDefinition beanDefinition = new GeneralBeanDefinition();
        beanDefinition.setFactoryBeanName("factory");
        beanDefinition.setFactoryMethodName("getInstanceUserService");
        beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETION);
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        Object userService1 = beanFactory.getBean("userService");
        System.out.println(userService1);
        Object userService2= beanFactory.getBean("userService");
        System.out.println(userService2);
        System.out.println(userService1==userService2);
    }

}
